<?php
class Model_Core_Abstract
    extends Object
{
    protected $_resourceModel;

    protected $_isNew = true;

    protected $_isDataChenge = false;

    protected $_primary;

    protected function _init($resourceModel)
    {
        if(!isset($resourceModel)) {
            throw new Zend_Exception('No set resource model in '.get_class($this));
        } elseif(is_string($resourceModel)) {
            $this->_resourceModel = new $resourceModel();
        } else {
            $this->_resourceModel = $resourceModel;
        }

        return $this;
    }

    public function getResource()
    {
        return $this->_resourceModel;
    }

    public function save()
    {
        $this->_beforeSave();
        try {
            $this->getResource()->save($this);
        } catch (Exception $e) {
            
        }
        $this->_afterSave();
    }

    protected function _beforeSave()
    {
        return $this;
    }

    protected function _afterSave()
    {
        return $this;
    }

    public function setOriginalData($key, $value = null)
    {
        parent::setData($key, $value);

        return $this;
    }

    public function setData($key, $value = null)
    {
        parent::setData($key, $value);
        $this->_isDataChenge = true;

        return $this;
    }

    public function setIsDataChange($flag)
    {
        $this->_isDataChenge = $flag;
        return $this;
    }

    public function getIsDataChange()
    {
        return $this->_isDataChenge;
    }

    public function getIsNew()
    {
        return $this->_isNew;
    }

    public function setIsNew($flag)
    {
        $this->_isNew = $flag;
        return $this;
    }

    public function load($value, $field = null)
    {
        $this->_beforeLoad();
        $this->getResource()->load($this, $value, $field);
        $this->_afterLoad();

        return $this;
    }

    protected function _beforeLoad()
    {
        return $this;
    }

    protected function _afterLoad()
    {
        return $this;
    }

    public function getId()
    {
        if(isset($this->_primary)) {
            return $this->getData($this->_primary);
        }

        return null;
    }

    public function setId($value)
    {
        if(isset($this->_primary)) {
            $this->setData($this->_primary, $value);
        } else {
            $this->setData('id', $value);
        }

        return $this;
    }

    public function setPrimary($primary)
    {
        $this->_primary = $primary;
        return $this;
    }

    public function delete()
    {
        $this->getResource()->deleteModel($this);
        return $this;
    }
}